package dir_sync;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Vector;

/**
 * 文件同步窗口。
 */
public class StartCopy extends JFrame {

    private final JPanel contentPane;
    private final JRadioButton forwardSyncButton;
    private final JRadioButton backwardSyncButton;
    /**
     * 源目录比较结果集AAA
     */
    private Vector<File> v_a;
    /**
     * 目标目录比较结果集AAA
     */
    private Vector<File> v_b;
    /**
     * 源目录路径字符创
     */
    private String dir_a;
    /**
     * 目标目录路径字符创
     */
    private String dir_b;
    /**
     * 复制进度状态栏，用来打印一些复制信息。
     */
    private final JLabel clb;
    /**
     * 错误日志文本框，用来打印错误日志。
     */
    private final JTextArea cta;
    private final JProgressBar progressBar;
    //    private final JButton compareButton;
    private CopyThread copyThread;
    private final JButton startSyncButton;
    private JButton syncCopyButton;


    /**
     * @param v_a 要设置的 v_a
     */
    public void setV_a(Vector<File> v_a) {
        this.v_a = v_a;
    }


    /**
     * @param v_b 要设置的 v_b
     */
    public void setV_b(Vector<File> v_b) {
        this.v_b = v_b;
    }

    /**
     * @param dir_a 要设置的 dir_a
     */
    public void setDir_a(String dir_a) {
        this.dir_a = dir_a;
    }


    /**
     * @param dir_b 要设置的 dir_b
     */
    public void setDir_b(String dir_b) {
        this.dir_b = dir_b;
    }


    public StartCopy(JButton compareButton, JButton syncCopyButton) {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                dispose();
            }
        });
        try {
            String kk = UIManager.getSystemLookAndFeelClassName();// 获得当前系统样式名称字符串。
            UIManager.setLookAndFeel(kk);// 加载样式。
            SwingUtilities.updateComponentTreeUI(this);// 应用给当前的窗口。
        } catch (Exception e) {
            System.err.println("ERROR: " + e.getMessage());
        }
        setTitle("文件同步");
        setVisible(true);
        setResizable(true);
        // setAlwaysOnTop(true);
        setBounds(300, 300, 800, 600);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.NORTH);
        panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.CENTER);
        panel_1.setLayout(new BorderLayout(0, 0));

        JPanel panel_3 = new JPanel();
        panel_3.setBorder(new TitledBorder(null, "同步选项",
                TitledBorder.LEADING, TitledBorder.TOP, null, null));
        FlowLayout flowLayout_1 = (FlowLayout) panel_3.getLayout();
        flowLayout_1.setAlignment(FlowLayout.LEFT);
        panel_1.add(panel_3, BorderLayout.NORTH);

        forwardSyncButton = new JRadioButton("顺序同步");
        forwardSyncButton.setToolTipText("按着原始目录同步目标目录的方向复制不相同的文件。");
        forwardSyncButton.setSelected(true);
        panel_3.add(forwardSyncButton);

        backwardSyncButton = new JRadioButton("反向同步");
        backwardSyncButton.setToolTipText("按着从目标目录复制到原始目录中不同的文件。");
        backwardSyncButton.setEnabled(false);
        panel_3.add(backwardSyncButton);

        JPanel panel_2 = new JPanel();
        FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
        flowLayout.setAlignment(FlowLayout.RIGHT);
        contentPane.add(panel_2, BorderLayout.SOUTH);

        startSyncButton = new JButton("开始同步");
        startSyncButton.setToolTipText("开始复制同步文件。");
        startSyncButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                if (forwardSyncButton.isSelected()) {
                    System.out.println("准备创建线程开始复制" + v_a.size());
                    copyThread = new CopyThread("copy", v_a, dir_a, dir_b, clb, cta,
                            progressBar, compareButton, startSyncButton, syncCopyButton);
                    copyThread.start();

                }
                if (backwardSyncButton.isSelected()) {
                    System.out.println("准备创建线程开始复制" + v_b.size());
                    CopyThread copyThread = new CopyThread("copy", v_b, dir_b, dir_a, clb, cta,
                            progressBar, compareButton, startSyncButton, syncCopyButton);
                    copyThread.start();
                }
            }
        });
        panel_2.add(startSyncButton);

        JButton cancelButton = new JButton("取消");
        cancelButton.setToolTipText("取消");
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (copyThread != null) {
                    copyThread.stopRun();
                }
                syncCopyButton.setEnabled(true);
                compareButton.setEnabled(true);
                dispose();
            }
        });
        panel_2.add(cancelButton);

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(forwardSyncButton);
        buttonGroup.add(backwardSyncButton);

        JPanel panel_4 = new JPanel();
        panel_1.add(panel_4, BorderLayout.CENTER);
        panel_4.setLayout(new BorderLayout(10, 10));

        progressBar = new JProgressBar();
        progressBar.setForeground(SystemColor.activeCaption);
        progressBar.setToolTipText("复制进度");
        progressBar.setStringPainted(true);
        progressBar.setEnabled(false);

        panel_4.add(progressBar, BorderLayout.NORTH);

        JScrollPane scrollPane = new JScrollPane();
        panel_4.add(scrollPane, BorderLayout.CENTER);

        cta = new JTextArea();
        cta.setText("操作日志，如果发生同步错误这里做一些错误记录。\r\n");
        scrollPane.setViewportView(cta);

        clb = new JLabel("请点击开始按钮");
        panel_4.add(clb, BorderLayout.SOUTH);
    }


}
